package com.hsnn.medstgmini.settlement.dao.impl;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.dao.HibernateSqlUtil;
import com.hsnn.medstgmini.settlement.dao.PostOrderDao;
import com.hsnn.medstgmini.settlement.model.SetAcctInfo;
import com.hsnn.medstgmini.settlement.model.SetAcctTransInfo;
import com.hsnn.medstgmini.settlement.model.SetBankAcctInfo;
import com.hsnn.medstgmini.settlement.model.SetPostOrder;
import com.hsnn.medstgmini.settlement.model.SetRelationship;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.ParseNumber;

@Repository
public class PostOrderDaoImpl implements PostOrderDao {
	@Autowired
	private HibernateSqlUtil hiberSQLUtil;
	
	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;

	private Session getSession() {
		Session session = sessionFactory.getCurrentSession();
		if (null == session) {
			session = sessionFactory.openSession();
		}
		return session;
	}
	/**
	 *@category 获取缴款单列表数据[采购中心端]
	 *@author 应晓川
	 *@date 2015年7月21日08:56:54
	 *@param page
	 */
	@Override
	public Pagination getPostOrderList(Pagination page) {
		StringBuffer sql = new StringBuffer();
		 sql.append(" select sp.post_order_code as postOrderCode,                 ")
	       .append("sp.post_order_name AS postOrderName,                          ") 
	       .append("sr.org_name as postName,                                      ") 
	       .append("sr.father_org_name AS recpName,                               ") 
	       .append("sp.post_amount as postAmount,                                 ") 
	       .append("sp.post_bank_name as postBankName,                            ") 
	       .append("sp.post_bank_acct as postBankAcct,                            ") 
	       .append("sp.post_bank_acct_holder as postBankAcctHolder,               ") 
	       .append("sp.post_remark as postRemark,                                 ") 
	       .append("Date_format(sp.post_datetime, '%Y-%m-%d %H:%i:%S') AS createDatetime, ") 
	       .append("sp.status AS status                                                  ")
			.append("    from                                                       ")
			.append("        set_postorder sp,                                      ")
			.append("        set_relationship sr                                    ")
			.append("    WHERE                                                      ")
			.append("         sr.org_id  = sp.post_org_id                           ") 
			.append("        and sr.father_org_id = sp.recp_org_id                  ")
			.append("        and sp.recp_org_id =:zxId                              ")
		    .append("        and sp.post_order_type =:postOrderType                 ")
		    .append("        and (sp.status='1' or  sp.status='2')                  ");
		 
		  String submitStartDatetime = page.getConditions().get("submitStartDatetime");
		  String submitEndDatetime =  page.getConditions().get("submitEndDatetime");
	
		if(page.getConditions().get("postOrderCode")!=null){
			sql.append(" and sp.post_order_code =:postOrderCode          ");
		}
		if(page.getConditions().get("status")!=null){
			sql.append(" and sp.status =:status                          ");
		}
		if(StringUtils.isNotBlank(submitStartDatetime)){
			sql.append(" and sp.post_datetime >=:submitStartDatetime     ");
			 page.getConditions().put("submitStartDatetime", submitStartDatetime+" 00:00:00");
		}
		if(StringUtils.isNotBlank(submitEndDatetime)){
			sql.append(" and sp.post_datetime <=:submitEndDatetime       ");
			 page.getConditions().put("submitEndDatetime", submitEndDatetime+" 23:59:59");
		}
		sql.append(" Order by createDatetime desc");
		return hiberSQLUtil.getSqlPageMapList(sql.toString(),page.getConditions(),page);
	}
	
	/**
	 *@category 获取需审核缴款单[采购中心端]
	 *@author 应晓川
	 *@date 2015年7月21日18:22:42
	 *@param page
	 */
	@Override
	public Pagination getAuditPostOrderListByCenter(Pagination page) {
		//String userId = '6ccb170a-2c4d-11e5-acdf-40f2e9d2c708';
		StringBuffer sql = new StringBuffer();
		    sql.append(" select sp.post_order_code as postOrderCode,                 ")
	       .append("sp.post_order_name AS postOrderName,                          ") 
	       .append("sr.org_name as postName,                                      ") 
	       .append("sr.father_org_name AS recpName,                               ") 
	       .append("sp.post_amount as postAmount,                                 ")
	       .append("sp.post_order_id as postOrderId,                                 ")
	       .append("Date_format(sp.post_datetime, '%Y-%m-%d %H:%i:%S') AS createDatetime ")
		   .append("    from                                                       ")
		   .append("        set_postorder sp,                                      ")
		   .append("        set_relationship sr                                    ")
		   .append("    WHERE                                                      ")
		   .append("         sr.org_id  = sp.post_org_id                           ") 
		   .append("        and sr.father_org_id = sp.recp_org_id                  ")
		   .append("        and sp.recp_org_id =:userId                            ")
		   .append("        and sp.post_order_type =:postOrderType                 ")
		   .append("        and sp.status='0'                                      ");
		    
		  String postOrderCode = page.getConditions().get("postOrderCode");
		  String submitStartDatetime = page.getConditions().get("submitStartDatetime");
		  String submitEndDatetime =  page.getConditions().get("submitEndDatetime");
		if(StringUtils.isNotBlank(postOrderCode)){
			sql.append(" and sp.post_order_code =:postOrderCode          ");
		}
		if(StringUtils.isNotBlank(submitStartDatetime)){
			sql.append(" and sp.post_datetime >=:submitStartDatetime     ");
			 page.getConditions().put("submitStartDatetime", submitStartDatetime+" 00:00:00");
		}
		if(StringUtils.isNotBlank(submitEndDatetime)){
			sql.append(" and sp.post_datetime <=:submitEndDatetime       ");
			 page.getConditions().put("submitEndDatetime", submitEndDatetime+" 23:59:59");
		}
		sql.append(" Order by createDatetime desc");
		 return hiberSQLUtil.getSqlPageMapList(sql.toString(),page.getConditions(),page);
	}
	
	/**
	 *@category 获取缴款单列表数据[医疗机构端]
	 *@author 应晓川
	 *@date 2015年7月21日13:28:21
	 *@param page
	 */
	@Override
	public Pagination getPostOrderListByHosp(Pagination page) {
		//String hospId =  "00231C2A-DEF3-4A6A-8948-9D851E8A202A";
		StringBuffer sql = new StringBuffer();
			 sql.append(" select sp.post_order_code as postOrderCode,                     ")
			       .append("sp.post_order_name AS postOrderName,                          ") 
			       .append("sr.org_name as postName,                                      ") 
			       .append("sr.father_org_name AS recpName,                               ") 
			       .append("sp.post_amount as postAmount,                                 ") 
			       .append("sp.post_bank_name as postBankName,                            ") 
			       .append("sp.post_bank_acct as postBankAcct,                            ") 
			       .append("sp.post_bank_acct_holder as postBankAcctHolder,               ") 
			       .append("sp.post_remark as postRemark,                                 ") 
			       .append("Date_format(sp.post_datetime, '%Y-%m-%d %H:%i:%S') AS createDatetime, ") 
			       .append("sp.status AS status                                                  ") 
				.append("    from                                                       ")
				.append("        set_postorder sp,                                      ")
				.append("        set_relationship sr                                    ")
				.append("    WHERE                                                      ")
				.append("         sr.org_id  = sp.post_org_id                           ")
				.append("         and sp.post_order_type  = '1'                         ")
				.append("        and sp.post_org_id = :hospId                           ");
			  String submitStartDatetime = page.getConditions().get("submitStartDatetime");
			  String submitEndDatetime =  page.getConditions().get("submitEndDatetime");
		
			 	if(page.getConditions().get("postOrderCode")!=null){
					sql.append(" and sp.post_order_code =:postOrderCode          ");
				}
				if(StringUtils.isNotBlank(submitStartDatetime)){
					sql.append(" and sp.post_datetime >=:submitStartDatetime     ");
					 page.getConditions().put("submitStartDatetime", submitStartDatetime+" 00:00:00");
				}
				if(StringUtils.isNotBlank(submitEndDatetime)){
					sql.append(" and sp.post_datetime <=:submitEndDatetime       ");
					 page.getConditions().put("submitEndDatetime", submitEndDatetime+" 23:59:59");
				}
				if(page.getConditions().get("status")!=null){
					sql.append(" and sp.status =:status          ");
				}
				sql.append(" Order by createDatetime desc");
		return hiberSQLUtil.getSqlPageMapList(sql.toString(),page.getConditions(),page);
	}
	
	/************医疗机构提交缴款单**************/
	/**
	 *@category 新增缴款单数据[医疗机构端]
	 *@author 应晓川
	 *@date 2015年7月21日13:28:21
	 *@param page
	 */
	@Override
	public void saveSetPostOrder(Pagination page) {
		SetPostOrder setPostOrder = (SetPostOrder) page.getForm();
		
		String updUser = page.getConditions().get("updUser");
		String id = page.getConditions().get("id");
		String postOrderType = page.getConditions().get("postOrderType");
		Timestamp ts = new Timestamp(System.currentTimeMillis()); 
		setPostOrder.setPostOrderId(UUID.randomUUID().toString());
		setPostOrder.setPostOrderCode("JK"+getDateNum()+getRandom());
		setPostOrder.setPostOrderType(ParseNumber.toShort(postOrderType));//缴款单类型是 医疗机构(1)
		setPostOrder.setPostOrgId(id);//缴款机构id - hospId
		setPostOrder.setCreateDatetime(ts);//缴款提交时间
		setPostOrder.setCreateUser(updUser);//缴款提交人
		setPostOrder.setStatus(Constants.POST_ORDER_STATUS_0);//缴款状态 0未审核
		setPostOrder.setUpdUser(updUser);//更新人
		setPostOrder.setUpdDatetime(ts);//更新时间
		getSession().save(setPostOrder);
		getSession().flush();
	}

//--------------核算中心审核缴款单 updateSetAcctinfoByHosp(),updateSetPostOrderByHosp(),addSetAccttransinfoByHosp---------------------------------
	
	/**
	 *@category 获取我的缴款单[核算中心端]
	 *@author 应晓川
	 *@date 2015年7月21日18:22:42
	 *@param page
	 */
	@Override
	public Pagination getPostOrderListBySetCenter(Pagination page) {
		StringBuffer sql = new StringBuffer();
		 sql.append(" select sp.post_order_code as postOrderCode,                     ")
	       .append("sp.post_order_name AS postOrderName,                          ") 
	       .append("sr.org_name as postName,                                      ") 
	       .append("sr.father_org_name AS recpName,                               ") 
	       .append("sp.post_amount as postAmount,                                 ") 
	       .append("sp.post_bank_name as postBankName,                            ") 
	       .append("sp.post_bank_acct as postBankAcct,                            ") 
	       .append("sp.post_bank_acct_holder as postBankAcctHolder,               ") 
	       .append("sp.post_remark as postRemark,                                 ") 
	       .append("Date_format(sp.post_datetime, '%Y-%m-%d %H:%i:%S') AS createDatetime, ") 
	       .append("sp.status AS status                                                  ") 
		.append("    from                                                       ")
		.append("        set_postorder sp,                                      ")
		.append("        set_relationship sr                                    ")
		.append("    WHERE                                                      ")
		.append("         sr.org_id  = sp.post_org_id                           ")
		.append("         and sp.post_order_type  = 2                           ")
		.append("        and sp.post_org_id = :userId                           ");
		  String submitStartDatetime = page.getConditions().get("submitStartDatetime");
		  String submitEndDatetime =  page.getConditions().get("submitEndDatetime");
	
	 	if(page.getConditions().get("postOrderCode")!=null){
			sql.append(" and sp.post_order_code =:postOrderCode          ");
		}
		if(StringUtils.isNotBlank(submitStartDatetime)){
			sql.append(" and sp.post_datetime >=:submitStartDatetime     ");
			 page.getConditions().put("submitStartDatetime", submitStartDatetime+" 00:00:00");
		}
		if(StringUtils.isNotBlank(submitEndDatetime)){
			sql.append(" and sp.post_datetime <=:submitEndDatetime       ");
			 page.getConditions().put("submitEndDatetime", submitEndDatetime+" 23:59:59");
		}
		if(page.getConditions().get("status")!=null){
			sql.append(" and sp.status =:status          ");
		}
		sql.append(" Order by createDatetime desc");
		 return hiberSQLUtil.getSqlPageMapList(sql.toString(),page.getConditions(),page);
	}
	
	/**
	 *@category 获取需审核医疗机构缴款单[核算中心端]
	 *@author 应晓川
	 *@date 2015年7月21日18:22:42
	 *@param page
	 */
	@Override
	public Pagination getAuditPostOrderListBySetCenter(Pagination page) {
		//String userId = '6ccb170a-2c4d-11e5-acdf-40f2e9d2c708';
		StringBuffer sql = new StringBuffer();
		 sql.append(" select sp.post_order_code as postOrderCode,                 ")
	       .append("sp.post_order_name AS postOrderName,                          ") 
	       .append("sr.org_name AS postName,                                      ") 
	       .append("sr.father_org_name AS recpName,                               ") 
	       .append("sp.post_amount as postAmount,                                 ")
	       .append("sp.post_order_id as postOrderId,                                 ")
	       .append("Date_format(sp.post_datetime, '%Y-%m-%d %H:%i:%S') AS createDatetime ") 
		.append("    from set_postorder sp                                      ")
		.append("         LEFT JOIN set_relationship sr ON sp.post_org_id = sr.org_id")	
		.append("    WHERE                                                      ")
		.append("             sp.post_order_type  = 1                           ")
		.append("         and sp.recp_org_id = :userId                           ");
		  String submitStartDatetime = page.getConditions().get("submitStartDatetime");
		  String submitEndDatetime =  page.getConditions().get("submitEndDatetime");
		if(StringUtils.isBlank(page.getConditions().get("orderPostStatus"))){
			sql.append(" and sp.status = 0  ");
		}else{
			page.getConditions().remove("orderPostStatus");
		}
	 	if(page.getConditions().get("postOrderCode")!=null){
			sql.append(" and sp.post_order_code =:postOrderCode          ");
		}
		if(StringUtils.isNotBlank(submitStartDatetime)){
			sql.append(" and sp.post_datetime >=:submitStartDatetime     ");
			 page.getConditions().put("submitStartDatetime", submitStartDatetime+" 00:00:00");
		}
		if(StringUtils.isNotBlank(submitEndDatetime)){
			sql.append(" and sp.post_datetime <=:submitEndDatetime       ");
			 page.getConditions().put("submitEndDatetime", submitEndDatetime+" 23:59:59");
		}
		sql.append(" Order by createDatetime desc");
//	
//		 sql.append(" select sp.post_order_code as postOrderCode,                   ")
//			.append("        sp.post_order_id AS postOrderId,                        ")
//		 	.append("        sr.father_org_name AS recpName,                        ")
//			.append("        sr.org_name AS postName,                               ")
//			.append("        sp.post_datetime AS postDatetime,                      ")
//			.append("        sp.post_amount as postAmount                           ")
//			.append(" from set_postorder sp,set_relationship sr                    ")
//			.append(" where sp.status = 0                                          ")
//			.append(" and sp.post_order_type = 1                                   ")
//			.append(" and sp.recp_org_id = sr.father_org_id                        ")
//			.append(" and sp.recp_org_id = :userId                                ");
		 return hiberSQLUtil.getSqlPageMapList(sql.toString(),page.getConditions(),page);
	}
	
	/**
	 *@category 更新医疗机构账户金额[医疗机构端]
	 *@author 应晓川
	 *@date 2015年7月21日18:22:42
	 *@param page
	 */
	@Override
	public void updateSetAcctinfoBySetCenter(Pagination page) {
		SetAcctInfo setAcctinfo = (SetAcctInfo) page.getForm();
		Timestamp ts = new Timestamp(System.currentTimeMillis()); 
		//更新余额 String转double
		Double outstandAmt = Double.valueOf(page.getConditions().get("outstandAmt"));
		//更新可用余额
		Double availableAmt = Double.valueOf(page.getConditions().get("availableAmt"));
		setAcctinfo.setOutstandAmt(outstandAmt);
		setAcctinfo.setOutstandAmtUpdDttm(ts);
		setAcctinfo.setAvailableAmt(availableAmt);
		setAcctinfo.setAvailableAmtUpdDttm(ts);
		setAcctinfo.setUpdUser(page.getConditions().get("updUser"));
		setAcctinfo.setUpdDatetime(ts);
		getSession().update(setAcctinfo);
		getSession().flush();
	}

	/**
	 *@category 核算中心更新 医疗机构提交的缴款单状态
	 *@author 应晓川
	 *@date 2015年7月21日18:24:07
	 *@param page
	 */
	@Override
	public void updateSetPostOrderByHosp(Pagination page) {
		SetPostOrder setPostOrder = (SetPostOrder) page.getForm();
		Timestamp ts = new Timestamp(System.currentTimeMillis()); 
		short status  = Short.parseShort(page.getConditions().get("status"));
		String updUser = page.getConditions().get("updUser");
		setPostOrder.setStatus(status);
		setPostOrder.setAuditStatus(updUser);
		setPostOrder.setCreateDatetime(ts);//缴款提交时间
		setPostOrder.setUpdUser(updUser);//更新时间
		setPostOrder.setUpdDatetime(ts);//更新人
		getSession().update(setPostOrder);
		getSession().flush();
	}

	
	/**
	 *@category 根据Id获取账户信息
	 *@author 应晓川
	 *@date 2015年7月21日16:53:59
	 *@param page
	 */
	@Override
	public List<SetBankAcctInfo> getSetBankAcctinfoById(String id) {
		Criteria criteria = getSession().createCriteria(SetBankAcctInfo.class);
		criteria.add(Restrictions.eq("auditStatus", Constants.BAK_STATUS_AUDIT_PASS_INT));
		criteria.add(Restrictions.eq("settleAcctId", id));
		List<SetBankAcctInfo> setBankAcctInfo =  criteria.list();
		if (setBankAcctInfo.size() > 0) {
			return setBankAcctInfo;
		}else{
			return null;
		}
		 
	}
	
	
	/**
	 *@category 根据Id获取父级id
	 *@author 应晓川
	 *@date 2015年7月22日15:02:23
	 *@param hospId
	 */
	@Override
	public SetRelationship getFatherOrgId(String id) {
		Criteria criteria = getSession().createCriteria(SetRelationship.class);
		criteria.add(Restrictions.eq("orgId", id));
		SetRelationship setRelationship=  (SetRelationship)criteria.uniqueResult();
		if(setRelationship !=null ){
			return setRelationship;
		}else{
			return null;
		}
	}
	
	
	/**
	 *@category 获取未审核缴款单数据
	 *@author 应晓川
	 *@date 2015年7月23日10:36:10
	 *@param String postOrderId-缴款单ID,String id-当前机构id
	 */
	@Override
	public Pagination getSetPostOrderByPage(Pagination page) {
		StringBuffer sql = new StringBuffer();
		sql.append("select sp.post_order_name AS postOrderName,     ")
		.append("sp.post_datetime as postDatetime,               ")
		.append("sp.post_bank_name as postBankName,              ")
		.append(" sp.post_amount as postAmount,                   ")
		.append(" sp.post_bank_acct as  postBankAcct,             ")
		.append(" sp.post_bank_acct_holder  as postBankAcctHolder,")
		.append(" sp.post_remark as postRemark,                     ")
		.append(" sp.post_order_id as postOrderId,                  ")
		.append(" sp.post_order_code as postOrderCode               ")
		.append(" FROM set_postorder sp , set_relationship sr       ")
		.append(" where  sr.father_org_id = sp.recp_org_id	       ")
		.append(" and  sp.status ='0' 	                           ")
		
		.append(" and sp.post_order_id =:postOrderId 		       ")
		.append(" and sr.father_org_id =:id 	               ");
	 return hiberSQLUtil.getSqlPageMapList(sql.toString(),page.getConditions(),page);
	}
	
	/**
	 *@category 获取未审核缴款单数据
	 *@author 应晓川
	 *@date 2015年7月23日10:36:10
	 *@param String postOrderId-缴款单ID
	 */
	@Override
	public SetPostOrder getSetPostOrderById(String id) {
		Criteria criteria = getSession().createCriteria(SetPostOrder.class);
		criteria.add(Restrictions.eq("postOrderId", id));
		SetPostOrder setPostOrder=  (SetPostOrder)criteria.uniqueResult();
		if(setPostOrder !=null ){
			return setPostOrder;
		}else{
			return null;
		}
	}
	

	
	/**
	 *@category 根据id获取缴款单数据
	 *@author 应晓川
	 *@date 2015年7月23日10:36:10
	 *@param String postOrderId-缴款单ID,String id-当前机构id
	 */
	@Override
	public SetPostOrder getPostOrderById(String id) {
		Criteria criteria = getSession().createCriteria(SetPostOrder.class);
		criteria.add(Restrictions.eq("postOrderId",id));
        SetPostOrder setPostOrder=  (SetPostOrder)criteria.uniqueResult();
		return setPostOrder;
	}

	
	/**
	 * @category 生成编号
	 * @date 2015年7月10日
	 * @return
	 */
	private String getDateNum() {
		String str = "";
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		str = format.format(date) + getRandom();
		return str;
	}
	/**
	 * @category 生成5位随机数不足补零
	 * @date 2015年7月10日
	 * @return
	 */
	private String getRandom() {
		int random = (int) Math.floor(Math.random() * 10000);
		return fillZero(random, 4);
	}
	/**
	 * @category 补零
	 * @return
	 */
	private String fillZero(int number, int length) {
		String f = "%0" + length + "d";
		return String.format(f, number);
	}
	
	/**
	 *@category 更新缴款单数据
	 *@author 应晓川
	 *@date 2015年7月23日12:49:47
	 *@param 
	 */
	@Override
	public void updateSetPostOrder(SetPostOrder setPostOrder) {
		getSession().update(setPostOrder);
		getSession().flush();
	}
	/**
	 *@category 根据id获取账户信息
	 *@author 应晓川
	 *@date 2015年7月23日18:16:33
	 *@param id
	 */
	@Override
	public SetAcctInfo getSetAcctInfoById(String id) {
		Criteria criteria = getSession().createCriteria(SetAcctInfo.class);
		criteria.add(Restrictions.eq("orgId", id));
		SetAcctInfo setAcctInfo=  (SetAcctInfo)criteria.uniqueResult();
		if(setAcctInfo !=null ){
			return setAcctInfo;
		}else{
			return null;
		}
	
		
	}
	
	/**
	 *@category 更新账户信息
	 *@author 应晓川
	 *@date 2015年7月23日18:38:13
	 *@param setAcctInfo
	 */
	@Override
	public void updateSetAcctInfo(SetAcctInfo setAcctInfo) {
		getSession().update(setAcctInfo);
		getSession().flush();
	}
	
	/**
	 *@category 新增账户收支明细
	 *@author 应晓川
	 *@date 2015年7月23日18:38:13
	 *@param setAcctTransInfo
	 */
	@Override
	public void saveSetAcctTransInfo(SetAcctTransInfo setAcctTransInfo) {
		getSession().save(setAcctTransInfo);
		getSession().flush();
	}

	
	
	/**
	 *@category 根据银行账户获取账户人和银行名
	 *@author 应晓川
	 *@date 2015年7月31日18:04:09
	 *@param getBankAcct
	 */
	@Override
	public SetBankAcctInfo getBankAcct(String getBankAcct) {
		Criteria criteria = getSession().createCriteria(SetBankAcctInfo.class);
		criteria.add(Restrictions.eq("bankAcctNo",getBankAcct));	
		SetBankAcctInfo setBankAcctInfo=  (SetBankAcctInfo)criteria.uniqueResult();
		return setBankAcctInfo;
	}
	/**
	 *@category 根据缴款单名称获取缴款单
	 *@author 周奇
	 *@date 2015年8月13日11:42:16
	 *@param postOrderName
	 */
	@Override
	public SetPostOrder getPostOrderByName(String postOrderName) {
		Criteria criteria = getSession().createCriteria(SetPostOrder.class);
		criteria.add(Restrictions.eq("postOrderName",postOrderName));
		List<SetPostOrder> list = criteria.list();
		if(list.size()>0){
			return (SetPostOrder)criteria.list().get(0);	
		}
		return null;
	}


}